package leetcode.editor.cn;

import java.util.Locale;

//[125]验证回文串
public class ValidPalindrome125{
public static void main(String[] args) {
//    String str = "09aA";
//    byte[] bytes = str.getBytes();
//    for (int i = 0; i < bytes.length; i++) {
//        System.out.println("ValidPalindrome125:main:"+bytes[i]);//48 57 97 65
//    }
//    char[] chars = str.toCharArray();
//    for (int i = 0; i < chars.length; i++) {
//        System.out.println("ValidPalindrome125:main:chars:"+chars[i]);//0 9 a A
//    }


   Solution solution = new ValidPalindrome125().new Solution();
   
}

  //leetcode submit region begin(Prohibit modification and deletion)
class Solution {
    public boolean isPalindrome2(String s) {
        if (s.length()<=1){
            return true;
        }
        char[] chars = s.toCharArray();
        int left = 0;
        int right = chars.length-1;
        while (left < right){
            //left ->>> cut illage word
            while (left < right && !Character.isLetterOrDigit(chars[left])){
                left ++;
            }
            while (left < right && !Character.isLetterOrDigit(chars[right])) {
                right--;
            }
            if (Character.toLowerCase(chars[left]) != Character.toLowerCase(chars[right])){
                return false;
            }
            left++;
            right--;
        }

        return true;
    }
    public boolean isPalindrome(String s) {
        //0 48 A 65 a 97
        if (s.length()<=1){
            return true;
        }
        //1 过滤 无效的字符
        byte[] bytes = s.getBytes();
        int left = 0;
        int right = bytes.length-1;

        while (left < right) {
            byte leftVal = bytes[left];
            byte rightVal = bytes[right];

            //去掉无效的 字符 ,放越界
            while ((left < right) &&!(leftVal>47 && leftVal < 58) && !(leftVal>64 && leftVal <(65+26))
            && !(leftVal >96 && leftVal< (97 + 26))){
                left++;
                leftVal = bytes[left];
            }

            while ((left < right) && !(rightVal>47 && rightVal < 58) && !(rightVal>64 && rightVal <(65+26))
                    && !(rightVal >96 && rightVal< (97 + 26))){
                right--;
                rightVal = bytes[right];
            }

            //已遍历完成
            if (left >= right)break;

            //比较
            if (leftVal == rightVal) {
                left++;
                right--;
                continue;
            }

            //比较 统一大小写
            if (leftVal < 97){
                leftVal = (byte) (leftVal + (97-65));
            }
            if (rightVal < 97){
                rightVal = (byte) (rightVal + (97-65));
            }
            if (leftVal != rightVal)return false;

            left++;
            right--;
        }
        return true;
    }
}
//leetcode submit region end(Prohibit modification and deletion)

}